package com.csxy.lsh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csxy.lsh.JwtUtils;
import com.csxy.lsh.R;
import com.csxy.lsh.entity.TOrder;
import com.csxy.lsh.exception.MyException;
import com.csxy.lsh.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/edeorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest  request){
        String memberId= JwtUtils.getMemberIdByJwtToken(request);

        if (StringUtils.isEmpty(memberId)){
            throw new MyException(20001,"请登录");

        }
        String orderNo = orderService.createOrder(courseId, memberId);
        return R.ok().data("orderId", orderNo);
    }

    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfoById(@PathVariable("orderNo") String orderNo){
        QueryWrapper<TOrder> wrapper=new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        TOrder tOrder = orderService.getOne(wrapper);
        return R.ok().data("item",tOrder);
    }

    // 查詢所有訂單
    @GetMapping("/list")
    public R list() {
        return R.ok().data("list", orderService.list());
    }
}
